#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>



struct ListNode {
    int val;
    struct ListNode* next;
};

typedef struct ListNode LNode;
struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)
    {
        return head;
    }
    LNode* new = NULL;
    LNode* pcur = head;
    LNode* prev = NULL;
    //LNode *del = NULL;
    while (pcur != NULL && pcur->val == val)
    {
        pcur = pcur->next;
    }
    new = pcur;
    prev = pcur;
    while (pcur)
    {
        if (pcur->val == val)
        {
            //del = pcur;
            //free(del);
            pcur = pcur->next;
            prev->next = pcur;
        }
        else
        {
            prev = pcur;
            pcur = pcur->next;
        }
    }
    return new;
}